package com.jie.dao.impl;

import com.jie.dao.Agency_relationDao;
import com.jie.entity.Agency_relation;
import com.jie.util.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by 12904 on 2018/8/6.
 */
public class Agency_relationDaoImpl implements Agency_relationDao
{
    @Override
    public int find(int said)
    {
        int count = 0;
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        String sql = "select COUNT(1) from agency_relation WHERE 1 = 1 ";
        sql += " and said = ?";
        conn = BaseDao.getConnection();
        try {
            stat = conn.prepareStatement(sql);
            stat.setInt(1,said);
            rs = stat.executeQuery();
            if (rs.next()){
               count = rs.getInt(1);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeConnection(conn,stat,rs);
        }
        return count;
    }

    public Agency_relation findByAid(int aid , int said)
    {
        Agency_relation relation = null;
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        String sql = "select * from agency_relation WHERE 1 = 1 ";
        if (aid != 0 && said == 0){
            sql += " and aid = ?";
        }else if (aid == 0 && said != 0){
            sql += " and said = ?";
        }
        conn = BaseDao.getConnection();
        try {
            stat = conn.prepareStatement(sql);
            if (aid != 0 && said == 0){
                stat.setInt(1,aid);
            }else if (aid == 0 && said != 0){
                stat.setInt(1,said);
            }
            rs = stat.executeQuery();
            if (rs.next()){
                relation = new Agency_relation();
                relation.setArid(rs.getInt("arid"));
                relation.setAid(rs.getInt("aid"));
                relation.setSaid(rs.getInt("said"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeConnection(conn,stat,rs);
        }
        return relation;
    }

    @Override
    public int del(int arid)
    {
        String sql = "delete from agency_relation where arid = ?";
        Object[] objects = {
                arid
        };
        return BaseDao.executeUpdate(sql,objects);
    }
}
